package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.PigHouseDAO;
import cn.edu.scau.cmi.hombio.dao.PigHouseTypeDAO;

import cn.edu.scau.cmi.hombio.domain.PigHouse;
import cn.edu.scau.cmi.hombio.domain.PigHouseType;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for PigHouseType entities
 * 
 */

@Service("PigHouseTypeService")

@Transactional
public class PigHouseTypeServiceImpl implements PigHouseTypeService {

	/**
	 * DAO injected by Spring that manages PigHouse entities
	 * 
	 */
	@Autowired
	private PigHouseDAO pigHouseDAO;

	/**
	 * DAO injected by Spring that manages PigHouseType entities
	 * 
	 */
	@Autowired
	private PigHouseTypeDAO pigHouseTypeDAO;

	/**
	 * Instantiates a new PigHouseTypeServiceImpl.
	 *
	 */
	public PigHouseTypeServiceImpl() {
	}

	/**
	 */
	@Transactional
	public PigHouseType findPigHouseTypeByPrimaryKey(Integer id) {
		return pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(id);
	}

	/**
	 * Load an existing PigHouseType entity
	 * 
	 */
	@Transactional
	public Set<PigHouseType> loadPigHouseTypes() {
		return pigHouseTypeDAO.findAllPigHouseTypes();
	}

	/**
	 * Delete an existing PigHouseType entity
	 * 
	 */
	@Transactional
	public void deletePigHouseType(PigHouseType pighousetype) {
		pigHouseTypeDAO.remove(pighousetype);
		pigHouseTypeDAO.flush();
	}

	/**
	 * Save an existing PigHouseType entity
	 * 
	 */
	@Transactional
	public void savePigHouseType(PigHouseType pighousetype) {
		PigHouseType existingPigHouseType = pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(pighousetype.getId());

		if (existingPigHouseType != null) {
			if (existingPigHouseType != pighousetype) {
				existingPigHouseType.setId(pighousetype.getId());
				existingPigHouseType.setName(pighousetype.getName());
				existingPigHouseType.setMemo(pighousetype.getMemo());
			}
			pighousetype = pigHouseTypeDAO.store(existingPigHouseType);
		} else {
			pighousetype = pigHouseTypeDAO.store(pighousetype);
		}
		pigHouseTypeDAO.flush();
	}

	/**
	 * Return a count of all PigHouseType entity
	 * 
	 */
	@Transactional
	public Integer countPigHouseTypes() {
		return ((Long) pigHouseTypeDAO.createQuerySingleResult("select count(o) from PigHouseType o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing PigHouse entity
	 * 
	 */
	@Transactional
	public PigHouseType deletePigHouseTypePigHouses(Integer pighousetype_id, Integer related_pighouses_id) {
		PigHouse related_pighouses = pigHouseDAO.findPigHouseByPrimaryKey(related_pighouses_id, -1, -1);

		PigHouseType pighousetype = pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(pighousetype_id, -1, -1);

		related_pighouses.setPigHouseType(null);
		pighousetype.getPigHouses().remove(related_pighouses);

		pigHouseDAO.remove(related_pighouses);
		pigHouseDAO.flush();

		return pighousetype;
	}

	/**
	 * Return all PigHouseType entity
	 * 
	 */
	@Transactional
	public List<PigHouseType> findAllPigHouseTypes(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<PigHouseType>(pigHouseTypeDAO.findAllPigHouseTypes(startResult, maxRows));
	}

	/**
	 * Save an existing PigHouse entity
	 * 
	 */
	@Transactional
	public PigHouseType savePigHouseTypePigHouses(Integer id, PigHouse related_pighouses) {
		PigHouseType pighousetype = pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(id, -1, -1);
		PigHouse existingpigHouses = pigHouseDAO.findPigHouseByPrimaryKey(related_pighouses.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigHouses != null) {
			existingpigHouses.setId(related_pighouses.getId());
			existingpigHouses.setNumber(related_pighouses.getNumber());
			existingpigHouses.setRfid(related_pighouses.getRfid());
			existingpigHouses.setMemo(related_pighouses.getMemo());
			related_pighouses = existingpigHouses;
		}

		related_pighouses.setPigHouseType(pighousetype);
		pighousetype.getPigHouses().add(related_pighouses);
		related_pighouses = pigHouseDAO.store(related_pighouses);
		pigHouseDAO.flush();

		pighousetype = pigHouseTypeDAO.store(pighousetype);
		pigHouseTypeDAO.flush();

		return pighousetype;
	}
}
